package defaultPackage;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;
public class Object
{
	int x,y,lastTileX,lastTileY;
	ObjectType type;
	boolean blocksAir, operational = true;
	Grid grid;
	private static int NEXT_ID = 1;
	private int id;
	private double randTrajectory = Math.random();
	boolean isBlocked = false;
	String designation;
	BufferedImage corpseSprite = null;
	public Object(int x,int y,ObjectType type,Grid grid)
	{
		designation = type.getDesignation();
		this.id=NEXT_ID;
		NEXT_ID++;
		this.grid = grid;
		this.x = x;
		this.y = y;
		setType(type);
		
		try {
			corpseSprite = ImageIO.read(URLClassLoader.getSystemResource("res/Sprites/"+"Corpse.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public int getID()
	{
		return id;
	}
	public void draw(Graphics g)
	{
		if (type == ObjectType.LifeSupport)
		{
			g.setColor(Color.GRAY);
			g.fillRect(x+2, y, 6, 10);
			g.setColor(Color.BLACK);
			g.fillRect(x+2, y+1, 6, 1);
			g.fillRect(x+2, y+3, 6, 1);
			g.setColor(Color.DARK_GRAY);
			g.drawRect(x+2,y,5,9);
		}
		if (type == ObjectType.ResourceGatherer)
		{
			g.setColor(Color.orange);
			g.fillRect(x+2, y, 6, 10);
			g.setColor(Color.BLACK);
			g.fillRect(x+2, y+1, 6, 1);
			g.fillRect(x+2, y+3, 6, 1);
			g.setColor(Color.DARK_GRAY);
			g.drawRect(x+2,y,5,9);
		}
		if (type == ObjectType.Corpse)
		{
			g.drawImage(corpseSprite, x, y, null);
		}
		if (type == ObjectType.Airlock)
		{
			g.setColor(Color.DARK_GRAY);
			g.fillRect(x, y, 10, 10);
			g.setColor(Color.LIGHT_GRAY);
			g.fillOval(x+1, y+1, 7, 7);
			g.setColor(Color.black);
			g.drawLine(x+3, y+3, x+3, y+6);
			g.drawLine(x+6, y+3, x+6, y+6);
		}
		if (type == ObjectType.Door)
		{
			if (grid.tiles[(x/10)][(y/10) + 1].getType() != tileType.Hull && grid.tiles[x/10][(y/10)-1].getType() != tileType.Hull)
			{
				if (blocksAir)
				{
					g.setColor(Color.DARK_GRAY);
					g.fillRect(x, y, 10, 10);
					g.setColor(Color.BLACK);
					g.drawLine(x+4, y, x+4, y + 10);
					g.drawLine(x+5, y, x+5, y + 10);
					g.drawRect(x, y, 10, 10);
				}
				else if (!blocksAir)
				{
					g.setColor(Color.DARK_GRAY);
					g.fillRect(x, y, 2, 9);
					g.fillRect(x+7, y, 2, 9);
					g.setColor(Color.black);
					g.drawRect(x, y, 2, 9);
					g.drawRect(x+7,y,2,9);
				}
			}
			else
			{
				if (blocksAir)
				{
					g.setColor(Color.DARK_GRAY);
					g.fillRect(x, y, 9, 9);
					g.setColor(Color.BLACK);
					g.drawLine(x, y+4, x+9, y + 4);
					g.drawLine(x, y+5, x+9, y + 5);
					g.drawRect(x, y, 9, 9);
				}
				else if (!blocksAir)
				{
					g.setColor(Color.DARK_GRAY);
					g.fillRect(x, y, 9, 2);
					g.fillRect(x, y+7, 9, 2);
					g.setColor(Color.black);
					g.drawRect(x, y, 9, 2);
					g.drawRect(x,y+7,9,2);
				}
			}
		}
	}
	
	public void setType (ObjectType type)
	{
		this.type = type;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
	
	public void setX(int x)
	{
		this.x = x;
	}
	
	public void setY(int y)
	{
		this.y = y;
	}
	
	public void setBlocksAir(boolean blocksAir)
	{
		this.blocksAir = blocksAir;
	}
	
	public boolean isBlockingAir()
	{
		return blocksAir;
	}

	public void setOperational(boolean operational)
	{
		this.operational = operational;
	}
	
	public boolean isOperational()
	{
		return operational;
	}
	
	public double getTileOxygen()
	{
		return grid.tiles[x/10][y/10].getOxygen();
	}
	
	public void setLastTileX(int lastX)
	{
		this.lastTileX = lastX;
	}
	
	public void setLastTileY(int lastY)
	{
		this.lastTileY = lastY;
	}
	
	public int getLastTileX()
	{
		return lastTileX;
	}
	
	public int getLastTileY()
	{
		return lastTileY;
	}
	
	public int getTrajectory()
	{
		if (randTrajectory >.5)
			return -1;
		else
			return 1;
	}
	
	public boolean isBlocked()
	{
		return isBlocked;
	}
	
	public void setBlocked(boolean blocked)
	{
		this.isBlocked = blocked;
	}
	
	public String getDesignation()
	{
		return designation;
	}
	public void setDesignation(String designation)
	{
		this.designation = designation;
	}
}
